﻿var ServiceNotifyMgr = {
    tick: "ss.notify.tick",
    notifyCheckEsp: 2000,
    notifyRunningStatus: false,
    startNotifyListener: function() {
        if (false == ServiceNotifyMgr.notifyRunningStatus) {
            ServiceNotifyMgr.notifyRunningStatus = true;
            window.setTimeout(ServiceNotifyMgr.handleNotify, ServiceNotifyMgr.notifyCheckEsp);
        }
    },
    stopNotifyListener: function() {
        if (true == ServiceNotifyMgr.notifyRunningStatus) {
            ServiceNotifyMgr.notifyRunningStatus = false;
        }
    },
    handleNotify: function () {
        if (true == ServiceNotifyMgr.notifyRunningStatus) {
            ServiceNotifyMgr.notifyRunning();
            window.setTimeout(ServiceNotifyMgr.handleNotify, ServiceNotifyMgr.notifyCheckEsp);
        }
    },
    setNotifyTick: function(tick) {
        setStorage(ServiceNotifyMgr.setNotifyTick, Number(tick));
    },
    getNotifyTick: function() {
        var tick = 0;
        var tickVal = getStorageVal(ServiceNotifyMgr.setNotifyTick);
        if (null != tickVal) {
            tick = Number(tickVal);
        }
        return tick;
    },
    notifyRunning: function () {
        var url = "/Notice/Query?userId="+UserInfoMgr.getUserID()+"tick=" + ServiceNotifyMgr.getNotifyTick();
        NativeAjax(null, url, function (msg) {
            var notice = JSON.parse(msg);
            if (notice.result == 0) {
                var messages = notice.messages;
                ServiceNotifyMgr.setNotifyTick(notice.tick);
                for (var index in messages) {
                    var item = messages[index];
                    if (item.type == "order_changed_notify") {
                        var notifyDate = item.content;
                        if (null != OrderMgr.orderTakeOffNotify) {
                            OrderMgr.orderTakeOffNotify(notifyDate.orderid);
                        }
                    }
                }
            }
        });
    },
};